package com.asin;

import com.asin.mapper.UserMapper;
import com.asin.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MainApp {
    public static void main(String[] args) throws IOException {
        //1.加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");

        //2.获取SqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //3.通过工厂对象获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        List<User> list = sqlSession.selectList("findAll");
        //4.通过动态代理获取UserMapper对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //5.执行方法
        List<User> userList = userMapper.findAll();
        for (User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }
}
